/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.ItemDAO;
import dto.Item;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author aptech
 */
public class ItemShop extends HttpServlet {

    ItemDAO dao = new ItemDAO();
 ArrayList<Item> itemList = dao.listItem;
    public ItemShop() {
        // if (dao != null) {
        //  }
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    void updateComment(ArrayList<Item> itemList, String code, String comment, HttpServletRequest request) {
        for (Item i : itemList) {
                    if (i.getCode().equals(code)) {
                        Item item = new Item(code, i.getName(), request.getParameter("comment").toString(), i.getPurcharseDaily());
                        dao.listItem.remove(i);
                        dao.listItem.add(item);


                    }
                }
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           
            String action = request.getParameter("action") == null ? "list" : request.getParameter("action");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ItemShop</title>");
            out.println("<script type='text/javascript' src='../../../web/js/ass3.js'></script>");
            out.println("</head>");
            out.println("<body>");
            if (action.equals("list")) {

                if (request.getParameter("update")!= null) {
                    String code = request.getParameter("code");
                    String name = request.getParameter("name");
                    String comment= request.getParameter("comment");
                    updateComment(itemList, code, comment, request);
                }
                for (Item item : itemList) {
                    out.println("<h2>Item</h2>");
                    out.println("<h3>Item code:" + item.getCode() + "</h3>");
                    out.println("<h3>Item name:" + item.getName() + "</h3>");
                    out.println("<h3>Item cooment:" + item.getComment() + "</h3>");
                    out.println("<h3>Purchased daily:" + item.getPurcharseDaily()+ "</h3>");
                    out.println("<a href=ItemShop?action=addComment&code=" + item.getCode() + ">comment</a>");
                }
            } else if (action.equals("addComment")) {
                String code = request.getParameter("code");

                Item item = dao.getByCode(code);
                out.println("<h2>Item</h2>");
                out.println("<h3>Item code:" + item.getCode() + "</h3>");
                out.println("<h3>Item code:" + item.getName() + "</h3>");
                String value = (item !=null)?item.getComment():"";
                out.println("<form  method='get' />");
                out.println("<input type='text' name='comment' value='"+ value +"' />");
                out.println("<input type='hidden' name='update' value='true' />");
                out.println("<input type='hidden' name='code' value='" + code + "' />");
               // out.println("<a href=ItemShop?action=updateComment&code=" + item.getCode() + "&comment=" + "FixedAssigned-line84" + ">update</a>");

                out.print("<input type='submit' value='Click to update' />");
                out.print("</form>");

            } 
//            else if (action.equals("ItemShop")) {
//                out.print("<h1>Updated</h1>");
//                String code = request.getParameter("code");
//
//                ArrayList<Item> itemList = dao.listItem;
//                String comment = request.getParameter("comment");
//                updateComment(itemList, code, comment , request);
//                for (Item i : itemList) {
//                    //  out.println("<h2>For</h2>");
//                    if (i.getCode().equals(code)) {
//                        out.println("<h2>Item</h2>");
//                        out.println("<h3>Item code:" + i.getCode() + "</h3>");
//                        out.println("<h3>Item name:" + i.getName() + "</h3>");
//                        out.println("<h3>Item comment:" + i.getComment() + "</h3>");
//                    }
//                }
//
//            }
//            out.println("</body>");
//            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
